//
//  SectionNearByDAetailVC.m
//  VTravel
//
//  Created by lanouhn on 16/8/3.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "SectionNearByDAetailVC.h"
#import "RootScrollViewModel.h"
#import "SectionNearByDetailCVC.h"
#import "NearByCellClickDetailVC.h"

@interface SectionNearByDAetailVC ()<UICollectionViewDelegate, UICollectionViewDataSource>

/** 集合视图 */
@property (nonatomic, strong) UICollectionView *collectionView;


@end

@implementation SectionNearByDAetailVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

/** 周边景点更多数据解析 */
- (void)readData{
    [NetWorkManager requestForGETWithURL:StrByAppendStr(KNearByMoreDetailListURL, city_id) paramater:nil success:^(id responseObject) {
        NSArray *array = responseObject;
        for (NSDictionary *dic in array) {            
            RootScrollViewModel *model = [[RootScrollViewModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.src = ImageURL(dic[@"src"]);
            [self.dataArray addObject:model];
        }
        // 刷新界面
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    } fail:^(NSError *error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self readData];
    });
    
    UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
    flowLayOut.itemSize = CGSizeMake((kScreenWidth - 8 * 3) / 2, 80);
    flowLayOut.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    flowLayOut.minimumLineSpacing = 8;
    flowLayOut.minimumInteritemSpacing = 8;
    flowLayOut.scrollDirection = UICollectionViewScrollDirectionVertical;
  
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayOut];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SectionNearByDetailCVC" bundle:nil] forCellWithReuseIdentifier:@"SectionNearByDetailCVC"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionNearByDetailCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SectionNearByDetailCVC" forIndexPath:indexPath];
    RootScrollViewModel *model = self.dataArray[indexPath.row];
    if (self.dataArray.count != 0) {
        [cell.Imagebutton sd_setBackgroundImageWithURL:[NSURL URLWithString:model.src] forState:UIControlStateNormal];
        [cell.Imagebutton setTitle:model.title forState:(UIControlStateNormal)];
        [cell.Imagebutton addTarget:self action:@selector(clickButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return cell;
}

- (void)clickButtonAction:(UIButton *)sender{
    kMyLog(@"%@", sender.titleLabel.text);
    NearByCellClickDetailVC *detailVC = [[NearByCellClickDetailVC alloc] init];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.TitleStr = sender.titleLabel.text;
    detailVC.type = XYPushTransitionTypeFromRootNearByCells;
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
